﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;

public partial class index : System.Web.UI.Page
{
    MSSQLHelp.SqlDbHelper _sqlhelp = new MSSQLHelp.SqlDbHelper();
    /// <summary>
    /// 网页加载事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = _sqlhelp.ExecuteDataSet("SMTP_GetList", CommandType.StoredProcedure, null);
        DataList1.DataSource = ds;
        DataList1.DataBind();
        string a = ds.Tables[0].Rows[0]["smtpmail"].ToString(); ;
        Response.Cookies["SMTP-DataSource"].Value = "dt";
        Response.Cookies["SMTP-DataSource"].Expires = DateTime.Now.AddHours(1);
    }
    /// <summary>
    /// SMTP邮件添加事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Bt_smtp_add_Click(object sender, EventArgs e)
    {
        //int i=_sqlhelp.ExecuteNonQuery("INSERT INTO SMTP (name,username,userpass) VALUES('@name','@username','@userpass')",CommandType.Text, new SqlParameter []{
        //new SqlParameter("@name",this.Tb_smtp_name.Text.Trim()),
        //new SqlParameter("@username",this.Tb_smtp_pass.Text.Trim()),
        //new SqlParameter("@userpass",this.Tb_smtp_username.Text.Trim())
        //});
        //Response.Write("<script>alert(" + i.ToString() + ")</script>");
        //Response.Redirect("index.aspx#tab-6");
        int i = _sqlhelp.ExecuteNonQuery("SMTP_ADD", CommandType.StoredProcedure, new SqlParameter[] { 
        new SqlParameter("@id",""),        
        new SqlParameter("@cid","1"),
        new SqlParameter("@name",this.Tb_smtp_name.Text.Trim()),
        new SqlParameter("@username",this.Tb_smtp_pass.Text.Trim()),
        new SqlParameter("@userpass",this.Tb_smtp_username.Text.Trim()),
        new SqlParameter("@userother","sdasda")
        });
        Response.Write("<script>alert(" + i.ToString() + ")</script>");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
    }
    /// <summary>
    /// 邮件发送按钮时间
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Bt_send_ok_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = _sqlhelp.ExecuteDataTable("SMTP_GetList", CommandType.StoredProcedure, null);
            string smtpadd = dt.Rows[0]["smtpmail"].ToString();
            string username = dt.Rows[0]["username"].ToString();
            string userpass = dt.Rows[0]["userpass"].ToString();
            SmtpClient _mysmtp = new SmtpClient(smtpadd);
            _mysmtp.Credentials = new System.Net.NetworkCredential(username, userpass);
            MailMessage _MsgMail = new MailMessage();
            _MsgMail.From = new MailAddress(username);
            _MsgMail.To.Add(this.Tb_send_mail.Text.Trim());
            _MsgMail.Subject = this.Tb_send_title.Text.Trim();
            AlternateView alt = AlternateView.CreateAlternateViewFromString(this.Tb_send_conten.Text.Trim(), null, "text/html");
            _MsgMail.AlternateViews.Add(alt);
            _mysmtp.Send(_MsgMail);
        }
        catch(Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
}